package com.wb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wb.reggie.common.BaseContext;
import com.wb.reggie.common.R;
import com.wb.reggie.entity.ShoppingCart;
import com.wb.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据,通过 BaseContext.getCurrentId()获取或者session获取
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前添加的菜品或套餐是否在购物车中，如果是，则数量+=1 （业务场景是，该菜品或套餐多次点击加入购物车）
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId != null){//添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{//添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //select * from shoppingcart where user_id = ? and dish_id/setmeal_id = ?   tips:dish_id/setmeal_id取其一
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);

        //如果存在，则数量+=1，再更新之
        if(shoppingCartOne != null){
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartOne);
        }else{
            //如果不存在，则添加到购物车，数量默认1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());//设置时间
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }


        return R.success(shoppingCartOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        log.info("查看购物车……");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据userId查询 (用户id在登录时，有设置在该线程中)
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //排序
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list();

        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new  LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 在页面只是点击减少时发出的请求，所以我这做修改只是对商品数量进行减少即可
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据,通过 BaseContext.getCurrentId()获取或者session获取
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前添加的菜品或套餐是否在购物车中，如果是，则数量+=1 （业务场景是，该菜品或套餐多次点击加入购物车）
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId != null){//添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{//添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //select * from shoppingcart where user_id = ? and dish_id/setmeal_id = ?   tips:dish_id/setmeal_id取其一
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);

        //如果存在，则数量+=1，再更新之
        if(shoppingCartOne != null){
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number - 1);
            shoppingCartService.updateById(shoppingCartOne);
        }else{
            //如果不存在，则添加到购物车，数量默认1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());//设置时间
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }


        return R.success(shoppingCartOne);
    }

}
